﻿using System;
using System.Web;
using Newtonsoft.Json;
using eCommerce.core.Helpers;
using eCommerce.db.BORespository;
using eCommerce.db.DataRespository;
using eCommerce.log;

namespace eCommerce.db.Action
{
    public class ExpenseAction : IBaseAction
    {
        public void Do(HttpContext context)
        {
            var actionName = Utils.GetIdFromRequestString(Utils.Action, string.Empty);

            var responseData = new ResponseData();
            switch (actionName)
            {
                case "search":
                    responseData = ListSearch();
                    break;

                case "save":
                    responseData = Save();
                    break;
                
                case "delete":
                    responseData = Delete();
                    break;

                case "":
                    responseData.Success = false;
                    responseData.Message = "Can not found this action.";
                    break;
            }

            var jSonString = JsonConvert.SerializeObject(responseData, Utils.SerializeFormat("MM/dd/yyyy HH:mm:ss"));
            if (!string.IsNullOrEmpty(context.Request.Params["callback"]))
                jSonString = string.Format("{0}({1})", context.Request.Params["callback"], jSonString);
            context.Response.Write(jSonString);
        }

        private static ResponseData ListSearch()
        {
            var responseData = new ResponseData();
            
            var storeId     = Utils.GetIdFromRequestString(Utils.StoreId, 0);                  
            var startDate   = Utils.GetIdFromRequestString(Utils.StartDate, string.Empty);
            var endDate     = Utils.GetIdFromRequestString(Utils.EndDate, string.Empty);
            var pageIndex   = Utils.GetIdFromRequestString(Utils.PageIndex, Constant.PageIndex);
            var pageSize    = Utils.GetIdFromRequestString(Utils.PageSize, Constant.PageSize);

            try
            {
                var totalRecords = 0;
                var lstExpense = ExpenseBo.Search(storeId, startDate, endDate, pageIndex, pageSize, ref totalRecords);

                foreach (var expense in lstExpense)
                {
                    expense.FormatCreatedOn = Utils.FormatIn_HHMM_DDMMYYYY(expense.CreatedOn);
                    expense.FormatEditedOn  = Utils.FormatIn_HHMM_DDMMYYYY(expense.EditedOn);
                }

                responseData.Success  = true;
                responseData.Data     = lstExpense;
                responseData.TotalRow = totalRecords;                                
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }

            return responseData;
        }

        private static ResponseData Save()
        {
            var responseData = new ResponseData();

            var id        = Utils.GetIdFromRequestString(Utils.Id, 0);
            var storeId   = Utils.GetIdFromRequestString(Utils.TotalValue, 0);
            var desc      = Utils.GetIdFromRequestString(Utils.Description, string.Empty);
            var money     = Utils.GetIdFromRequestString(Utils.Percent, 0);
            var userName  = HttpContext.Current.User.Identity.Name;

            try
            {
                ExpenseEntity o = null;
                if (id > 0)
                {
                    o = ExpenseBo.GetById(id);
                    o.ExpensId      = id;
                    o.StoreId       = storeId;
                    o.Description   = desc;
                    o.Money         = money;
                    o.EditedBy      = userName;                                 

                    ExpenseBo.Update(o);                        
                }
                else
                {
                    o = new ExpenseEntity();
                    o.StoreId       = storeId;
                    o.Description   = desc;
                    o.Money         = money;
                    o.CreatedBy     = userName;                                 

                    ExpenseBo.Create(o);                        
                }
                
                responseData.Success  = true;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
            return null;
        }

        private static ResponseData Delete()
        {
            var responseData = new ResponseData();

            var id = Utils.GetIdFromRequestString(Utils.ExpensId, 0);

            try
            {
                ExpenseBo.Delete(id);

                responseData.Success  = true;                                
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }
    }
}
